﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TCEasyPrintRev.Models;
using TCEasyPrintRev.WCF_File;

namespace TCEasyPrintRev.Controllers
{ 
    public class FileController 
    {
        private static string autoIncrement()
        {
            string akhir;
            int angka;
            try{
            using (var session = CRUD<File>.load().OpenSession())
            {
                akhir = session.QueryOver<File>().List().Last().ID;

                
            }

            
            
            
             string[] x =  akhir.Split('F').ToArray(); angka = int.Parse(x[1]) + 1; }

            catch { angka= 1; }

            


            if (angka.ToString().Count() == 1)
                return "F0000" + angka;
            else if(angka.ToString().Count()==2)
                return "F000" + angka;
            else if(angka.ToString().Count()==3)
                return "F00" + angka;
            else if (angka.ToString().Count() == 4)
                return "F0" + angka;
            else
                return  angka.ToString();
        }

      
        public static String insert(FileS file)
        {
            
            file.ID = autoIncrement();
            file.Tanggal = System.DateTime.Now.ToString("MMM ddd d HH:mm yyyy");
            File baru = new File(file);
            CRUD<File>.insert(baru);
            return file.ID;
        }

        public static File getFileByID(string id)
        {
            using (var session = CRUD<File>.load().OpenSession())
            {
                return session.QueryOver<File>().List().Where(x => x.ID == id).Last();

            }
        }
    }
}